<template>
  <h3 style="margin-bottom: 20px">活动编辑</h3>
  <el-form
      ref="ruleFormRef"
      :model="marketing"
      label-width="120px"
      style="width: 600px;"
  >
    <el-form-item label="菜品">
      <el-select v-model="marketing.cuisineId">
        <el-option v-for="cuisine in cuisinesRef" :label="cuisine.name" :value="cuisine.id" :key="cuisine.id"/>
      </el-select>
    </el-form-item>
<!--    <el-form-item label="要求等级">-->
<!--      <el-input v-model="marketing.requireGrade"></el-input>-->
<!--    </el-form-item>-->
<!--    <el-form-item label="消耗积分">-->
<!--      <el-input v-model="marketing.depleteIntegration"></el-input>-->
<!--    </el-form-item>-->
    <el-form-item label="折扣">
      <el-input v-model="marketing.discount"></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submitForm(ruleFormRef)">保存</el-button>
    </el-form-item>
  </el-form>
</template>

<script setup>
import {onMounted, ref} from "vue";
import say from "../../util/say";
import {Fetch} from "../../api/request";
import {listCuisinesProApi} from "../../api";

let cuisinesRef = ref([])
onMounted(async () => {
  let cuisines = await (await listCuisinesProApi()).json()
  for (let v of cuisines.data) {
    let id = v.id
    let name = v.name
    let price = v.price
    let description = v.description
    cuisinesRef.value.push({id, name,price,description})
  }
})

let ruleFormRef = ref(null);
let marketing = ref({
  cuisineId: null,
  requireGrade: 0,
  depleteIntegration: 0,
  discount: 0.9,
});

const submitForm = async (formElement) => {
  if (!formElement) return
  await formElement.validate(async (valid) => {
    if (valid) {
      Fetch.post('/activity/save', marketing.value).then(response => {
        if (response.ok) {
          say.saySuccess('保存成功')
        } else {
          say.sayError('保存失败')
        }
      }).catch(_ => {
        say.sayError('保存失败')
      });
    } else {
      say.sayError('保存失败')
    }
  })
}
</script>

<style scoped>

</style>